package com.example.lab2;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatTextView;

import android.net.Uri;
;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button btn_play;
    private Button btn_enter;
    private Button btn_stop;
    private MediaPlayer mPlayer = null;
    Uri path;
    String url = "https://wsaudiobssdlbig.kugou.com/2004160844/H2oKU9y1dj3qY7cKwSHoEw/1587084275/bss/extname/wsaudio/2b2b281f88debd232acdd7cc87b99dea.mp3";




    private boolean isRelease = true;   //Flag to determine whether mediaplayer is released
    private boolean isStop = true; //Flag to determine whether to suspend release

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindViews();//Binding buttons and monitors

        path = Uri.parse((String) url);
        download();//Download Audio


    }

    private void bindViews() {
        btn_play = (Button) findViewById(R.id.btn_play);
        btn_enter = (Button) findViewById(R.id.btn_enter);
        btn_stop = (Button) findViewById(R.id.btn_stop);



        btn_play.setOnClickListener(this);
        btn_enter.setOnClickListener(this);
        btn_stop.setOnClickListener(this);

        btn_play.setEnabled(false);
        //btn_enter.setEnabled(false);
        btn_stop.setEnabled(false);
    }






    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_play:
                if(isStop == true) {
                    onPlayClick();
                }else{
                    onPauseClick();
                }
                break;
            case R.id.btn_enter:
                EditText urltext = (EditText)findViewById(R.id.editurl);
                String i = urltext.getText().toString();
                boolean a = (( i.equals("")) ||(i.equals("\n"))||(i.equals(" "))||(i.equals("\r")));
                TextView txt_xv = (TextView)findViewById(R.id.textView);
                if(!a) {
                    txt_xv.setText("true");
                    url = i;
                    onStopClick();
                    MarqueeText paoma = (MarqueeText)findViewById(R.id.textView3);
                    paoma.setText("自选歌曲                         自选歌曲                  ");



                    //txt_xv.setText(url);

                }
                break;
                /*
                        if(!(( i.equals("")) ||(i.equals("\n"))||(i.equals(" "))||(i.equals("\r"))))
                        {
                            ;
                            TextView txt_xv = (TextView)findViewById(R.id.textView2);
                            txt_xv.setText(urltext.getText().toString());
                            onStopClick();
                        }
                */



            case R.id.btn_stop:

                onStopClick();
                break;
        }
    }

    public  void onStopClick()
    {
    mPlayer.reset();     //Reset MediaPlayer
    mPlayer.release();   // release MediaPlayer
    isRelease = true;
    btn_play.setEnabled(false);
    //btn_enter.setEnabled(false);
    btn_stop.setEnabled(false);
    isStop = true;
    btn_play.setText("PLAY");
    download();
    }

    public void onPlayClick()
    {
        mPlayer.start();   //start play
        isStop = false;
        btn_play.setEnabled(true);
        //btn_enter.setEnabled(true);
        btn_stop.setEnabled(true);
        btn_play.setText("PAUSE");
    }
    public void onPauseClick()
    {
        mPlayer.pause();     //stop play
        btn_play.setEnabled(true);
        //btn_enter.setEnabled(false);
        btn_stop.setEnabled(true);
        isStop = true;
        btn_play.setText("PLAY");
    }
    public void download(){
        try {
            //mPlayer = MediaPlayer.create(this,R.raw.my);
            mPlayer = new MediaPlayer();
            //mPlayer.reset();

            mPlayer.setDataSource(url);
            TextView txt_xy = (TextView)findViewById(R.id.textView);
            txt_xy.setText(url);
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            //mPlayer.prepare();
            TextView txt_xv = (TextView)findViewById(R.id.textView2);
            txt_xv.setText("DOWNLOADING");
            mPlayer.prepareAsync();
            mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    TextView txt_xv = (TextView)findViewById(R.id.textView2);
                    txt_xv.setText("SOUND IS ALREADY");
                    //mp.start();
                    btn_play.setEnabled(true);
                    //btn_enter.setEnabled(false);
                    btn_stop.setEnabled(true);
                }
            });
            mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if(mp != null)
                    {
                        mp.release();
                        btn_play.setEnabled(true);
                        //btn_enter.setEnabled(false);
                        btn_stop.setEnabled(false);
                        isRelease = true;
                        isStop = true;
                        btn_play.setText("PLAY");
                        TextView txt_xv = (TextView)findViewById(R.id.textView2);
                        txt_xv.setText("SOUND END");
                        download();

                    }
                    Log.d("wyj","onCompletion :play sound.");
                }
            });
            mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Log.d("wjj","on error" + what + ","+extra );
                    return false;

                }
            });
            isRelease = false;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

